# python -m project_root.api.api_alarm
from .api_token import Token 
from util.time_util import timestran
import requests


class Api_Alarm(Token):


    def get_alarmlist(self,start_at,end_at):
        url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/list'
        json = {
            'limit':10,
            'page':1,
            'start_time':start_at,
            'end_time':end_at
        }
        response = requests.post(url=url,json=json,headers=self.get_header())
        values = []
        keys = ['alarm_at','device_name','app_scen_name','desc','level','return_status','confirm_status']
        for data in response.json()['data']['list']:
            for key in keys:
                if key == 'alarm_at':
                    values.append(timestran(data[key]))
                elif key == 'level':
                    if data[key] == 1:
                        values.append('预警')
                    else:
                        values.append('报警')
                elif key == 'return_status':
                    if data[key] == 1:
                        values.append('已复归')
                    else :
                        values.append('未复归')
                elif key == 'confirm_status':
                    if data[key] == 1:
                        values.append('已确认')
                    else:
                        values.append('未确认')
                else:
                    values.append(data[key])
        return values

    def get_alarm_details_list(self):
        url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/list'
        json = {
            'all_region_code':[],
            'dateRange':["",""],
            'limit':10,
            'page':1
        }
        values = []
        response = requests.post(url=url,json=json,headers=self.get_header())
        data = response.json()['data']['list'][0]
        keys = ['device_name','alarm_at','top_region_name','app_scen_name','desc','return_status','return_at',
                'return_by','return_desc','confirm_status','confirm_at','confirm_by','confirm_desc']
        for key in keys:
            if key in ['alarm_at','return_at','confirm_at']:
                values.append(timestran(data[key]))
            elif key == 'return_status':
                if data[key] == 1:
                    values.append('已复归')
                else:
                    values.append('未复归')
            elif key == 'confirm_status':
                if data[key] == 1:
                    values.append('已确认')
                else:
                    values.append('未确认')
            else:
                values.append(data[key])
        if data['return_status'] == 2 and data['confirm_status'] == 2:
            indices = [0, 1, 2, 3, 4, 5, 9] 
        elif data['return_status'] == 1 and data['confirm_status'] == 2:
            indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
        elif data['return_status'] == 2 and data['confirm_status'] == 1:
            indices = [0, 1, 2, 3, 4, 5, 9, 10, 11, 12] 
        else:
            indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 
        result = [values[i] for i in indices]
        return result





# ap = Api_Alarm()
# print(ap.get_alarm_details_list())